#include <QFileDialog>
#include <QUrl>
#include <QSettings>
#include "testwidget.h"
#include "ui_testwidget.h"

TestWidget::TestWidget(QWidget *parent) :
    QWidget(parent),
    m_setting("config.ini",QSettings::IniFormat),
    ui(new Ui::TestWidget)
{
    ui->setupUi(this);

    ui->comboBox_url->setCurrentText(m_setting.value("last_file").toString());

    for(int i=0;i<16;i++){
        QFrame* frame;
        if(i==0){
            frame=ui->frame_1;
        }else if(i==1){
            frame=ui->frame_2;
        }else if(i==2){
            frame=ui->frame_3;
        }else if(i==3){
            frame=ui->frame_4;
        }else if(i==4){
            frame=ui->frame_5;
        }else if(i==5){
            frame=ui->frame_6;
        }else if(i==6){
            frame=ui->frame_7;
        }else if(i==7){
            frame=ui->frame_8;
        }else if(i==8){
            frame=ui->frame_9;
        }else if(i==9){
            frame=ui->frame_10;
        }else if(i==10){
            frame=ui->frame_11;
        }else if(i==11){
            frame=ui->frame_12;
        }else if(i==12){
            frame=ui->frame_13;
        }else if(i==13){
            frame=ui->frame_14;
        }else if(i==14){
            frame=ui->frame_15;
        }else if(i==15){
            frame=ui->frame_16;
        }

        VideoPlayer* player = new VideoPlayer(QString("Player%1").arg(i+1),this);
        player->setWId(frame->winId());

        VideoControl* ctl = new VideoControl();
        ctl->player=player;
        ctl->frame=frame;
        m_videoCtls.push_back(ctl);
    }
}

TestWidget::~TestWidget()
{
    delete ui;
}

void TestWidget::on_pushButton_file_clicked()
{
    QString url = QFileDialog::getOpenFileName(this);
    ui->comboBox_url->setCurrentText(url);

    m_setting.setValue("last_file",url);
}

void TestWidget::on_pushButton_start_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player->play(media);
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player->play(media);
    }
}

void TestWidget::on_pushButton_pause_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player->pause();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player->pause();
    }
}

void TestWidget::on_pushButton_resume_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player->resume();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player->resume();
    }
}

void TestWidget::on_pushButton_stop_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    MediaInfo media;
    media.url=ui->comboBox_url->currentText();

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player->stop();
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player->stop();
    }
}

void TestWidget::on_pushButton_seek_clicked()
{
    QString text=ui->comboBox_frame->currentText();
    qint64 pos = ui->doubleSpinBox_pos->value()*1000;

    if(text=="全部"){
        for(int i=0;i<m_videoCtls.size();i++){
            m_videoCtls[i]->player->seek(pos);
        }
    }else{
        int index = text.toInt()-1;
        m_videoCtls[index]->player->seek(pos);
    }
}
